import { RouteRecordNormalized } from 'vue-router'

const recursionRouter = (default_routes: Array<RouterType>, FaPath?: string): RouterType[] => {
  return default_routes.map((item: RouterType) => {
    item.path = `${FaPath ? FaPath : ''}${item.path.indexOf('/') === 0 ? '' : '/'}${item.path}`
    if (item.children && item.children.length) {
      recursionRouter(item.children, item.path)
    }
    return item
  })
}
export const getDefaultRouter = (routes: RouteRecordNormalized[]): Array<RouterType> => {
  const default_result = routes.filter(item => {
    const num = item.path.split('/')
    if (num.length - 1 === 1 && item.meta.icon && item.meta.title && !item.meta.hidden) {
      return (
        (item.children.length && !item.components) || (!item.children.length && item.components)
      )
    }
  })
  return default_result.map(item => {
    let obj: RouterType = {
      path: item.path,
      name: item.name as string,
      meta: item.meta,
      children: item.children as Array<RouterType>,
      components: item.components
    }
    return obj
  })
}

/**
 * @return {Router}
 * **/
export const GetAsyncRouter = (routes: RouteRecordNormalized[]): RouterType[] => {
  const res = getDefaultRouter(routes)
  return recursionRouter(res)
}
